import { query,queryPipe,queryDesignPipe,queryFlowMeter,queryWorkstations1 } from '@/services/issue_map';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import find from 'lodash/find'
import moment from 'moment';
import {
  message
} from 'antd';

export default {
  namespace: 'issue_map',
  state: {
    pageLoaded:false,
    data:[],
    pipeData:{},
    pipeLength:0,
    pipeCount:0,
    flowMeterData:[],
    Workstations1Data:[],
    designPipeData:{},
    searchParams:{},
    mapType: 'sensing',
    showMapType: false,
    showDepth: false,
    pressVisiable:true,
    flowMeterVisiable:true,
    pipeVisiable:true,
    designPipeVisiable:true,
    workstations1Visiable:true,
    designCheckedKeys:['design1'],
    zoom:document.body.offsetWidth>1700?14.3:13.5,
    pointData:{},
    flowMeterPointData:{},
    center:[89.21655514667722, 42.9558836514785],
    layers: [
      new TileLayer({
        title: '天地图卫星影像',
        visible: true,
        source: new XYZ({
          url: 'http://t3.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=136bb6f589f0402621a72ea362c9cb37',
        }),
      }),
      new TileLayer({
        title: '天地图矢量图',
        visible: false,
        source: new XYZ({
          url: 'http://t3.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=136bb6f589f0402621a72ea362c9cb37',
        }),
      }),
      new TileLayer({
        title: '卫星标注',
        visible: true,
        source: new XYZ({
          url: 'http://t4.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=136bb6f589f0402621a72ea362c9cb37',
        }),
      }),
      new TileLayer({
        title: '矢量图标注',
        visible: false,
        source: new XYZ({
          url: 'http://t4.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=136bb6f589f0402621a72ea362c9cb37',
        }),
      }),

    ],
    loading: true,
  },
  effects: {
    *changeMapType({ payload, callback }, { call, put,select }) {
      const state = yield select(state => state.issue_map)//获取state中的数据
      if (payload === 'sensing') {
        state.layers[3].setVisible(false);
        state.layers[2].setVisible(true);
        state.layers[1].setVisible(false);
        state.layers[0].setVisible(true);
      } else if (payload === 'road') {
        state.layers[3].setVisible(true);
        state.layers[2].setVisible(false);
        state.layers[1].setVisible(true);
        state.layers[0].setVisible(false);
      }
      yield put({
        type: 'saveMapType',
        payload:payload
      });
    },
    *changeCenterAndZoom({ payload, callback }, { call, put,select }) {
      yield put({
        type: 'saveCenterAndZoom',
        payload:payload
      });
    },
    *changeDesignCheckenKeys({ payload, callback }, { call, put,select }) {
      yield put({
        type: 'saveDesignCheckenKeys',
        payload:payload
      });
      setTimeout(function() {
        if(callback) callback();
      },0)
    },
    *changeShowMapType({ payload, callback }, { call, put,select }) {
      yield put({
        type: 'saveShowMapType',
        payload:payload
      });
    },
    *changeShowDepth({ payload, callback }, { call, put,select }) {
      yield put({
        type: 'saveShowDepth',
        payload:payload
      });
    },
    *changePressLayerVisiable({ payload, callback }, { call, put,select }) {
      yield put({
        type: 'savePressLayerVisiable',
        payload:payload
      });
      setTimeout(function() {
        if(callback) callback();
      },0)
    },
    *changeFlowMeterVisiable({ payload, callback }, { call, put,select }) {
      yield put({
        type: 'saveFlowMeterLayerVisiable',
        payload:payload
      });
      setTimeout(function() {
        if(callback) callback();
      },0)
    },
    *changePipeLayerVisiable({ payload, callback }, { call, put,select }) {
      console.log('payload',payload)
      yield put({
        type: 'savePipeLayerVisiable',
        payload:payload
      });
      setTimeout(function() {
        if(callback) callback();
      },0)
    },
    *changeDesignPipeLayerVisiable({ payload, callback }, { call, put,select }) {
      console.log('payload',payload)
      yield put({
        type: 'saveDesignPipeLayerVisiable',
        payload:payload
      });
      setTimeout(function() {
        if(callback) callback();
      },0)
    },
    *reset({ payload, callback }, { call, put,select }) {
      console.log('reset=============')
      const state = yield select(state => state.issue_map)//获取state中的数据
      yield put({
        type: 'resetData',
        payload:{
          pageLoaded:payload,
          data:state.data
        }
      });
      if(callback) callback()
    },
    *changeLoaded({ payload, callback }, { call, put }) {
      yield put({
        type: 'changePageLoaded',
        payload: true,
      });
    },
    *fetchPointData({ payload, callback }, { call, put ,select}) {
      console.log('fetchPointData')
      const state = yield select(state => state.issue_map)//获取state中的数据
      const response = find(state.data,(o)=>{
        return o.id===payload.id
      });
      if(response){
        yield put({
          type: 'savePointData',
          payload:  {...response}
        });
        if(callback) callback()
      }
    },
    *fetchFlowMeterPointData({ payload, callback }, { call, put ,select}) {
      console.log('fetchFlowMeterPointData')
      const state = yield select(state => state.issue_map)//获取state中的数据
      const response = find(state.flowMeterData,(o)=>{
        return o.id===payload.id
      });
      if(response){
        yield put({
          type: 'saveFlowMeterPointData',
          payload:  {...response}
        });
        if(callback) callback()
      }
    },
    *fetchPipe({ payload,callback }, { call, put ,select}) {
      const pipeResponse = yield call(queryPipe, payload);
      console.log('pipeResponse',pipeResponse)

      if(pipeResponse.status===200){
        if(typeof pipeResponse.data === 'string'){
          console.log('加载管网出错')
          message.error('加载现状管网出错')
          return  false
        }
        yield put({
          type: 'savePipe',
          payload:  pipeResponse.data
        });

        if (callback) callback();
      }
    },
    *fetchDesignPipe({ payload,callback }, { call, put ,select}) {
      const designPipeResponse = yield call(queryDesignPipe, payload);
      console.log('designPipeResponse',designPipeResponse)
      if(designPipeResponse.status===200){
        if(typeof designPipeResponse.data === 'string'){
          console.log('加载设计管网出错')
          message.error('加载设计管网出错')
          return  false
        }
        yield put({
          type: 'saveDesignPipe',
          payload:  designPipeResponse.data
        });

        if (callback) callback();
      }
    },
    *fetch({ payload,callback }, { call, put ,select}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const state = yield select(state => state.issue_map)//获取state中的数据
      payload=payload||state.searchParams;
      const workstations1Response = yield call(queryWorkstations1, payload);
      const response = yield call(query, payload);
      const flowMeterResponse = yield call(queryFlowMeter, payload);

      if(response.data.status_code===200){
        yield put({
          type: 'save',
          payload:  response.data
        });
        yield put({
          type: 'saveFlowMeter',
          payload:  flowMeterResponse.data
        });
        yield put({
          type: 'saveWorkstations1',
          payload:  workstations1Response.data
        });

        yield put({
          type: 'saveSearchParams',
          payload: payload
        });

        yield put({
          type: 'changeLoading',
          payload: false,
        });
        if (callback) callback();
      }


    },
  },

  reducers: {

    saveShowMapType(state, action){
      return {
        ...state,
        showMapType: action.payload,
      };
    },
    saveShowDepth(state, action){
      return {
        ...state,
        showDepth: action.payload,
      };
    },
    saveDesignCheckenKeys(state, action){
      return {
        ...state,
        designCheckedKeys: action.payload,
      };
    },
    savePressLayerVisiable(state, action){
      return {
        ...state,
        pressVisiable: action.payload,
      };
    },
    saveFlowMeterLayerVisiable(state, action){
      return {
        ...state,
        flowMeterVisiable: action.payload,
      };
    },
    saveWorkstations1Visiable(state, action){
      return {
        ...state,
        workstations1Visiable: action.payload,
      };
    },
    savePipeLayerVisiable(state, action){
      console.log('action.payload',action.payload)
      return {
        ...state,
        pipeVisiable: action.payload,
      };
    },
    saveDesignPipeLayerVisiable(state, action){
      console.log('action.payload',action.payload)
      return {
        ...state,
        designPipeVisiable: action.payload,
      };
    },
    saveMapType(state, action){
      return {
        ...state,
        mapType: action.payload,
      };
    },
    changeCenterAndZoom(state, action){
      return {
        ...state,
        center: action.payload.center,
        zoom: action.payload.zoom,
      };
    },
    save(state, action) {
      for(let i=0;i<action.payload.data.length;i++){
        action.payload.data[i].my_time=String(Math.random()*10)
      }
      return {
        ...state,
        data: action.payload.data,
        meta:action.payload.meta,
        additional:action.payload.additional
      };
    },
    saveFlowMeter(state, action) {
      for(let i=0;i<action.payload.data.length;i++){
        action.payload.data[i].my_time=String(Math.random()*10)
      }
      return {
        ...state,
        flowMeterData: action.payload.data,
      };
    },
    saveWorkstations1(state, action) {
      return {
        ...state,
        Workstations1Data: action.payload.data,
      };
    },
    savePipe(state, action) {
      let pipeLength=0;
      for(let i=0;i<action.payload.features.length;i++){
        pipeLength+=Number(action.payload.features[i].properties['长度'].toFixed(3))
      }
      return {
        ...state,
        pipeData: action.payload,
        pipeCount:action.payload.features.length,
        pipeLength:pipeLength.toFixed(2)
      };
    },
    saveDesignPipe(state, action) {
      return {
        ...state,
        designPipeData: action.payload,
      };
    },
    savePointData(state, action) {
      return {
        ...state,
        pointData: {...action.payload,my_time:moment().format('YYYY-MM-DD HH:mm:ss')},
      };
    },
    saveFlowMeterPointData(state, action){
      return {
        ...state,
        flowMeterPointData: {...action.payload,my_time:moment().format('YYYY-MM-DD HH:mm:ss')},
      };
    },
    changePageLoaded(state, action) {
      return {
        ...state,
        pageLoaded: action.payload,
      };
    },
    resetData(state, action) {
      return  {
        pageLoaded:action.payload.pageLoaded,
        data:action.payload.data,
        pipeData:{},
        flowMeterData:[],
        Workstations1Data:[],
        designPipeData:{},
        searchParams:{},
        mapType: 'sensing',
        showMapType: false,
        showDepth:false,
        pressVisiable:true,
        flowMeterVisiable:true,
        pipeVisiable:true,
        designPipeVisiable:true,
        workstations1Visiable:true,
        zoom:document.body.offsetWidth>1700?14.3:13.5,
        pointData:{},
        flowMeterPointData:{},
        designCheckedKeys:['design1'],
        center:[89.21655514667722, 42.9558836514785],
        layers: [
          new TileLayer({
            title: '天地图卫星影像',
            visible: true,
            source: new XYZ({
              url: 'http://t3.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=136bb6f589f0402621a72ea362c9cb37',
            }),
          }),
          new TileLayer({
            title: '天地图矢量图',
            visible: false,
            source: new XYZ({
              url: 'http://t3.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=136bb6f589f0402621a72ea362c9cb37',
            }),
          }),
          new TileLayer({
            title: '卫星标注',
            visible: true,
            source: new XYZ({
              url: 'http://t4.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=136bb6f589f0402621a72ea362c9cb37',
            }),
          }),
          new TileLayer({
            title: '矢量图标注',
            visible: false,
            source: new XYZ({
              url: 'http://t4.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=136bb6f589f0402621a72ea362c9cb37',
            }),
          }),

        ],
        loading: true,
      };
    },
  },

};
